package com.cci.kangdao.utilTool;

import com.alibaba.fastjson.JSONObject;
import com.cci.kangdao.crmapi.CRMApiUtils;
import com.cci.kangdao.crmapi.CRMResponeEntity;
import com.cci.kangdao.dao.CRMPlatformServiceDao;
import com.cci.kangdao.dao.CompanyTDao;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.WorkOrderT;
import com.cci.kangdao.redis.RedisClientUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: shiliqiang
 * @company: CTY Technology Co.,Ltd
 * @since: 2021/7/28 14:46
 * @version: V1.0
 */
public class ThreadHandlerUtil implements Runnable{
    private Logger log = Logger.getLogger(this.getClass().getName());

    private List<JSONObject> list;
    private Long orderId;
    private WorkOrderTDao workOrderTDao;
    private CompanyTDao companyTDao;
    private CRMPlatformServiceDao platformServiceDao;
    private RedisClientUtils redisClientUtils;

    public ThreadHandlerUtil(List list, Long orderId, WorkOrderTDao workOrderTDao, CompanyTDao companyTDao, CRMPlatformServiceDao platformServiceDao, RedisClientUtils redisClientUtils){
        this.list = list;
        this.orderId = orderId;
        this.workOrderTDao = workOrderTDao;
        this.companyTDao = companyTDao;
        this.platformServiceDao = platformServiceDao;
        this.redisClientUtils = redisClientUtils;
    }

    @Override
    public void run() {
        WorkOrderT workOrder = workOrderTDao.getWorkOrderByOrderId(orderId);
        if (workOrder != null) {
            String orderNo = workOrder.getOrderNo();
            for (JSONObject obj : list) {
                obj.put("orderNo", workOrder.getOrderNo());
            }
            //parameters.put("faults", list);
            //parameters.put("faults", JSONArray.parseArray(JSON.toJSONString(list)));
            //parameters.put("orderNo", workOrder.getOrderNo());
            //parameters.put("CRMNo", workOrder.getOrderNoCrm());
            CRMResponeEntity crmResponeEntity = CRMApiUtils.getCRMServiceApi().syncFaultInfoToCRM(
                    companyTDao.getCompanyInfoByCompanyID(workOrder.getOrderChannel()).getCompanyNo(),
                    platformServiceDao, redisClientUtils, list, orderNo);
        } else {
            log.info("没有根据orderId="+orderId+"查询到对应工单");
        }

    }
}
